package com.manor.paradise.activity

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.net.Uri
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.manor.common.activity.AbsActivity
import com.manor.common.custom.BorderRoundTransformation
import com.manor.common.http.JHttpCallback
import com.manor.common.utils.*
import com.manor.paradise.R
import com.manor.paradise.adapter.ReikIndexAdapter
import com.manor.paradise.bean.ReikindexBean
import com.manor.paradise.http.ElvesHttpConst
import com.manor.paradise.http.ElvesHttpUtil
import com.blankj.utilcode.util.SpanUtils
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.request.RequestOptions
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.yanzhenjie.permission.Action
import com.yanzhenjie.permission.AndPermission
import com.yanzhenjie.permission.runtime.Permission
import kotlinx.android.synthetic.main.activity_it_invite.*

/**
 * 获取下级直推灵气指数列表信息
 */
class ItInviteActivity : AbsActivity() {
    internal lateinit var mAdapter: ReikIndexAdapter
    private var pageNum = 1
    private val mDataList = mutableListOf<ReikindexBean.RecordsBean>()
    private var mineBean: ReikindexBean.RecordsBean? = null
    private var mId = ""


    companion object {
        fun forward(context: Context, bean: ReikindexBean.RecordsBean) {
            val intent = Intent(context, ItInviteActivity::class.java)
            intent.putExtra("mineBean", bean)
            context.startActivity(intent)
        }
    }

    override fun getLayoutId(): Int = R.layout.activity_it_invite

    override fun main() {
        super.main()
        mineBean = intent?.getSerializableExtra("mineBean") as ReikindexBean.RecordsBean?
        mId = mineBean?.id ?: ""

        if (StringUtil.isEmpty(mId)) {
            ToastUtil.show("信息错误")
            finish()
        }
        initMine()
        im_back.setOnClickListener { finish() }

        mRecycler.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
        mAdapter = ReikIndexAdapter(R.layout.item_reiki_index_new, mDataList) //item_reiki_index
        mRecycler.adapter = mAdapter
        mAdapter.addChildClickViewIds(R.id.ll_tel)
        mAdapter.setOnItemChildClickListener(itemChildClickListener)
        refreshLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onLoadMore(refreshLayout: RefreshLayout) {
                pageNum++
                setData(false)
            }

            override fun onRefresh(refreshLayout: RefreshLayout) {
                pageNum = 1
                setData(true)
            }
        })
        setData(true)
    }


    override fun onDestroy() {
        this@ItInviteActivity.let { Glide.get(it).clearMemory() }
        ElvesHttpUtil.cancel(ElvesHttpConst.POST_LOWER_LIST_INFO)
        super.onDestroy()
    }

    private fun initMine() {
        mineBean?.let { item ->
            val req = RequestOptions()
                    .transform(
                            CenterCrop(),
                            CircleCrop(),
                            BorderRoundTransformation(this@ItInviteActivity,
                                    im_av.width / 2, 0, 5, Color.WHITE, 4)
                    )

            Glide.with(this@ItInviteActivity)
                    .load(item.avatar)
                    .apply(req)
                    .into(im_av)

            tv_id.text = "淘米号：" + item.serialNo
            tv_rtiem.text = "注册时间：" + item.createTime

            tv_id.text = "淘米号：" + item.serialNo
            tv_id.text = "淘米号：" + item.serialNo

            setSpan(tv_anima, item.anima.toString(), "个人活跃")
            setSpan(tv_tribal, item.teamLiveness.toString(), "团队活跃度")
            setSpan(tv_subordinates, item.lowerHireCount.toString() + "/" + item.lowerCount.toString(), "已孵/已邀")

            setLevel(item.star, tvLevel, ivLevel, bl_lay)

        }
    }


    private fun setSpan(tv: TextView, content: String, title: String) {
        SpanUtils.with(tv)
                .appendLine(content)
                .append(title)
                .setForegroundColor(Color.parseColor("#66ffffff"))
                .setFontSize(12, true)
                .create()
    }

    private fun setLevel(star: String, tv: TextView, img: ImageView, lay: LinearLayout) {
        //(-1无 0准星 1一星 2二星 3三星)
        when (star) {
            "0" -> {
                tv.text = "一星农场主"
                img.setBackgroundResource(R.mipmap.pic_y_jz0)
            }
            "1" -> {
                tv.text = "二星农场主"
                img.setBackgroundResource(R.mipmap.pic_y_jz1)
            }
            "2" -> {
                tv.text = "三星农场主"
                img.setBackgroundResource(R.mipmap.pic_y_jz2)
            }
            "3" -> {
                tv.text = "四星农场主"
                img.setBackgroundResource(R.mipmap.pic_y_jz3)
            }
//            "4" -> {
//                tv.text = "璀璨钻石"
//                img.setBackgroundResource(R.mipmap.pic_y_jz4)
//            }
            else -> {
                lay.setVisible(false)
                img.setVisible(false)
            }
        }
    }


    fun setData(isRefresh: Boolean) {
        ElvesHttpUtil.getLowerInviteList(pageNum, mId, object : JHttpCallback() {
            override fun onSuccess(code: Int, msg: String, info: String) {
                if (code != 200) {
                    ToastUtil.show(msg)
                    onError()
                    return
                }
                var reikindexBean: ReikindexBean = GsonUtil.gsonToBean<ReikindexBean>(info, ReikindexBean::class.java)
                KLog.e("json", reikindexBean)
                if (pageNum == 1) {
                    mDataList.clear()
                    mDataList.addAll(reikindexBean.records)
                    mAdapter.notifyDataSetChanged()
                    refreshLayout.finishRefresh()
                } else {
                    mDataList.addAll(reikindexBean.records)
                    mAdapter.notifyDataSetChanged()
                    refreshLayout.finishLoadMore()
                }
            }


            override fun onError() {
                super.onError()
                if (!isRefresh) {
                    pageNum--
                    refreshLayout.finishLoadMore()
                }
            }
        })
    }


    private val itemChildClickListener = OnItemChildClickListener { adapter, view, position ->
        val id = view.id
        //KLog.e("tag",communityDynamicAdapter.getItem(position).getUserinfo().getUser_nickname()+"/"+position);
        if (id == R.id.ll_tel) {
            callPhone(mAdapter.getItem(position).mobile)
        }
    }
}